#include<stdio.h>
int arr[200006];
int main() {
  int len;
  scanf("%d",&len);
  for(int i=0;i<len;++i) {
    scanf(" %d",arr+i);
  }

  long long cnt=0;
  for(int i=1;i<len;++i) {
    int *cur=arr+i, *prev=arr+i-1;
    if(*prev > *cur) {
      cnt += *prev - *cur;
      *cur = *prev;
    }
  }
  printf("%ld\n",cnt);
  return 0;
}
